package com.taoism.utils;

import java.util.Random;

/**
 * 生成随机数工具类
 *
 * @author liujunxia
 * @version v0.0.1
 * @date 2018年1月12日下午6:04:14
 */
public class RandomUtils {

    /**
     * 随机生成两位或三位数字的字符串
     *
     * @return
     * @author liujunxia
     * @date 2018年1月12日下午6:15:32
     * @version v0.0.1
     */
    public static String randomToString() {
        return (int) ((Math.random() * 9) * 100) + "";
    }

    /**
     * 生成指定位数的随机验证码
     * @param randomNumer
     * @return
     */
    public static String verifyCode(Integer randomNumer) {
        Random random = new Random();
        String str = "";
        for (int i = 0; i < randomNumer; i++){
            int key = random.nextInt(3);
            switch (key){
                case 0:
                    int code1 = random.nextInt(10);
                    str += code1;
                    break;
                case 1:
                    char code2 = (char)(random.nextInt(26)+65);
                    str += code2;
                    break;
                case 2:
                    char code3 = (char)(random.nextInt(26)+97);
                    str += code3;
                    break;
            }
        }
        return str;
    }

    /**
     * 获取随机字符串，由数字、大小写字母组成
     * @param bytes：生成的字符串的位数
     * @return
     * @author
     */
    public static String getRandomStr(int bytes){
        StringBuilder sb = new StringBuilder();
        Random random = new Random();
        for (int i = 0; i < bytes; i++) {
            //随机判断判断该字符是数字还是字母
            String choice = random.nextInt(2) % 2 == 0 ? "char" : "num";
            if ("char".equalsIgnoreCase(choice)) {
                //随机判断是大写字母还是小写字母
                int start = random.nextInt(2) % 2 == 0 ? 65 : 97;
                sb.append((char) (start + random.nextInt(26)));
            } else if ("num".equalsIgnoreCase(choice)) {
                sb.append(random.nextInt(10));
            }
        }
        return sb.toString();
    }
}
